package view;

import utils.Tools;

import javax.swing.*;

public class MainView {

    private JFrame frame;
    private JMenuBar all;
    private JMenu file;
    private JMenu classmenu;
    private JMenuItem Newclass;
    private JMenuItem classset;
    private JMenu group;
    private JMenuItem Newgroup;
    private JMenuItem grouplist;
    private JMenu student;
    private JMenuItem Newstudent;
    private JMenuItem studentlist;
    private JMenu suiji;
    private JMenuItem suijibanji;
    private JMenuItem suijixiaozu;
    private JMenuItem suijixuesheng;
    private final int WIDTH = 400;
    private final int HEIGHT = 300;

    public MainView(){
        frame = new JFrame();
        initView();
        frame.setTitle("学生信息管理系统主页");

        frame.setLayout(null);//空布局
        frame.setVisible(true);//可视
        frame.setResizable(false);//改变大小
        //关闭并退出，主窗口
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Tools.setPos(frame,WIDTH,HEIGHT);
        frame.validate();//使组件生效
    }
    private void initView(){
        JPanel jPanel = new JPanel();

        all = new JMenuBar();
        // 创建并设置文件菜单
        file = new JMenu("文件");

        // 创建并设置班级菜单及其子项
        classmenu = new JMenu("班级管理");
        Newclass = new JMenuItem("新建班级");
        classset = new JMenuItem("班级列表");
        classmenu.add(Newclass);
        classmenu.add(classset);
        // 为班级管理菜单项添加监听器
        Newclass.addActionListener(e -> {
            frame.setVisible(false);
            new AddClassView();
        });
        classset.addActionListener(e -> {
            frame.setVisible(false);
            new ClassListView();
        });


        // 创建并设置小组菜单及其子项
        group = new JMenu("小组管理");
        Newgroup = new JMenuItem("新建小组");
        grouplist = new JMenuItem("小组列表");
        group.add(Newgroup);
        group.add(grouplist);
        // 为小组管理菜单项添加监听器
        Newgroup.addActionListener(e -> {
            frame.setVisible(false);
            new AddGroupView();
        });

        grouplist.addActionListener(e -> {
            frame.setVisible(false);
            new GroupListView();
        });


        // 创建并设置学生菜单及其子项
        student = new JMenu("学生管理");
        Newstudent = new JMenuItem("新增学生");
        studentlist = new JMenuItem("学生列表");
        student.add(Newstudent);
        student.add(studentlist);
        // 为学生管理菜单项添加监听器
        Newstudent.addActionListener(e -> {
            frame.setVisible(false);
            new AddStudentView();
        });

        studentlist.addActionListener(e -> {
            frame.setVisible(false);
            new StudentListView();
        });


        // 创建并设置随机抽取菜单及其子项
        suiji = new JMenu("随机点名");
        suijibanji = new JMenuItem("随机抽取班级");
        suijixiaozu = new JMenuItem("随机抽取小组");
        suijixuesheng = new JMenuItem("随机随机学生");
        suiji.add(suijibanji);
        suiji.add(suijixiaozu);
        suiji.add(suijixuesheng);
        // 为随机点名菜单项添加监听器
        suijibanji.addActionListener(e -> {
            frame.setVisible(false);
            new RandomClassView();
        });
        suijixiaozu.addActionListener(e ->{
            frame.setVisible(false);
            new RandomGroupView();
        });
        suijixuesheng.addActionListener(e -> {
            frame.setVisible(false);
            new RandomStudentView();
        });


        // 将所有主菜单添加到菜单栏
        all.add(file);
        all.add(classmenu);
        all.add(group);
        all.add(student);
        all.add(suiji);

        // 设置菜单栏到窗口
        frame.setJMenuBar(all);

        jPanel.setBounds(0, 0, WIDTH, HEIGHT);
        jPanel.setLayout(null);
        frame.add(jPanel);
    }

    }


